package com.yaglyf.mvc.service.core.impl;

import com.yaglyf.mvc.controller.vo.User;
import com.yaglyf.mvc.controller.vo.UserRegister;
import com.yaglyf.mvc.dao.entity.UserInfo;
import com.yaglyf.mvc.dao.mapper.UserInfoMapper;
import com.yaglyf.mvc.emun.UserEnum;
import com.yaglyf.mvc.service.core.intf.IUserInfoService;
import com.yaglyf.mvc.service.dto.UserDto;
import com.yaglyf.mvc.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * Title: Class CommentsShow
 * Description:
 * 注释展示类，说明各种方法注释怎么写
 *
 * @author qiang.yang
 * @version 1.0.0
 * @email qiang.yang@chinaredstar.com
 * @date: 2016/10/14 16:10
 */
@Service
public class UserInfoService implements IUserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public UserDto hasUser(User user) {
        UserDto userDto = new UserDto();
        UserInfo userInfo = userInfoMapper.getUserByName(user.getUserName());
        if(null != userInfo){
            String passWord = MD5.security(user.getPassWord(), userInfo.getSalt());
            if (userInfo.getUserPassword().equals(passWord)){
                userDto.setUserInfo(userInfo);
                return userDto.setUserEnum(UserEnum.LOGIN_SUC);
            }else {
              return userDto.setUserEnum(UserEnum.LOGIN_PSW_ERR);
            }
        }
        return userDto.setUserEnum(UserEnum.LOGIN_NAME_ERR);
    }

    @Override
    public UserDto userRegister(UserRegister userRegister) {
        UserDto userDto = new UserDto();
        UserInfo userInfo = userInfoMapper.getUserByName(userRegister.getRegUserName());
        if (null != userInfo){
            return userDto.setUserEnum(UserEnum.REG_NAME_ERR);
        }else {
            UserInfo use = new UserInfo();
            use.setUserName(userRegister.getRegUserName());
            use.setUserEmail(userRegister.getRegEmail());
            String salt = UUID.randomUUID().toString();
            use.setSalt(salt);
            String s = MD5.security(userRegister.getRegPassWord(), salt);
            use.setUserPassword(s);
            userInfoMapper.insertSelective(use);
            userDto.setUserInfo(use);
            return userDto.setUserEnum(UserEnum.REG_SUC);
        }
    }
}
